package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车数据
     * @param shoppingCartDTO
     * @return
     */
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //1.购物车中是否有要添加的商品数据
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //注意:方便复用所以用List接收 正常在此处只会查到一条数据
        List<ShoppingCart> shoppingCartList =shoppingCartMapper.list(shoppingCart);
        if(!CollectionUtils.isEmpty(shoppingCartList)){
            //2.如果有商品数据修改商品数量 +1
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateNumber(shoppingCart);
            return;
        }
        //3.如果没有创建购物车对象
        //4.给购物车对象赋值
        //4.1判断商品是菜品还是套餐
        if (shoppingCart.getDishId()!=null){
            //4.2如果是菜品 查询菜品表数据 赋值
            Dish dish = dishMapper.getById(shoppingCart.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        }else {
            //4.3如果套餐 查询套餐表数据 赋值
            Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        //5.调用mapper插入购物车数据
        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> list() {
        return shoppingCartMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }


    /**
     * 清空购物车
     * @return
     */
    public void clean() {
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }

    /**
     * 减去购物车中的一个商品
     * @param shoppingCartDTO
     * @return
     */
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //1.根据条件查询购物车数据
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //注意:方便复用所以用List接收 正常在此处只会查到一条数据
        List<ShoppingCart> shoppingCartList =shoppingCartMapper.list(shoppingCart);
        //2.判断购物车中的数量 数量大于一 就数量减一
        if (!CollectionUtils.isEmpty(shoppingCartList)){
            shoppingCart = shoppingCartList.get(0);
            if (shoppingCart.getNumber()!=1){
                //2.判断购物车中的数量 数量大于一 就数量减一
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCartMapper.updateNumber(shoppingCart);
                return;
            }else {
                //3.如果数量等于一就删除这条数据
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }
        }

    }
}
